/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.articles.importFromLn.articlesData.pesta;

/**
 *
 * @author x211800
 */
public class ColumnOfAuthors extends Columns {

    private static final String ANGULARBRACKETLEFT = "[";
    private static final String ANGULARBRACKETRIGHT = "]";
    private static ColumnOfAuthors instance;
    private String parsedAuthors = null;

    private ColumnOfAuthors() throws ColumnsError {
    }

    public static ColumnOfAuthors instanceOf(String columnContent) throws ColumnsError {
        if (instance == null) {
            instance = new ColumnOfAuthors();
        }

        instance.parseColumn(columnContent);
        return instance;
    }


    /**
     *
     * @param columns[1]
     * @return String{parsed author name with angular brackets}
     * @throws ColumnsError{when there is more name then one (the input string contains semicolon)}
     */
    private void parseColumn(String column) throws ColumnsError {
        String name = null;
        String columnWithoutTextDelimits = removeTextDelimits(column);
        if (columnWithoutTextDelimits != null) {
            if (columnWithoutTextDelimits.contains(SEMICOLON)) {
                throw new ColumnsError("multiple name problem");
            } else {
                if (columnWithoutTextDelimits.contains(SLASH)) {
                    name = replaceSlashes(columnWithoutTextDelimits);
                    if (name == null) {
                        throw new ColumnsError("problem with slashes");
                    }
                } else {
                    name = columnWithoutTextDelimits;
                }
            }
        }
        parsedAuthors = name;
    }


    /*
     * replacing slashes by angular brackets in name
     */
    private static String replaceSlashes(String n) {

        StringBuffer name = new StringBuffer(n);

        if (name.substring(name.indexOf(SLASH) + 1, name.length()).contains(SLASH)) {

            name.replace(name.indexOf(SLASH), name.indexOf(SLASH) + 1, ANGULARBRACKETLEFT);
            name.replace(name.indexOf(SLASH), name.indexOf(SLASH) + 1, ANGULARBRACKETRIGHT);

            if (name.toString().contains(SLASH)) {
                return null;
            }
        } else {
            return null;
        }

        //System.out.println(name);

        return name.toString();
    }


    public String getParsedAuthors() {
        return parsedAuthors;
    }
}
